package com.ldgk.pim.client.view.gwt;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Viewport;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.AdapterField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.toolbar.SeparatorToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.maps.client.geom.LatLng;
import com.ldgk.pim.client.presenter.MainPresenter;
import com.ldgk.pim.client.presenter.MapPresenter;
import com.ldgk.pim.client.presenter.Presenter;
import com.ldgk.pim.client.util.Util;
import com.ldgk.pim.client.view.MainView;
import com.ldgk.pim.client.view.MapView;

public class MainViewImpl implements MainView{
	private MainPresenter presenter;
	private MapView mapView;
	
	public static final BorderLayout layout = new BorderLayout();

	private final ContentPanel mainPanel = new ContentPanel();
	private final TextField<String> searchCriteria = new TextField<String>();

	public void setPresenter(Presenter presenter){
		this.presenter = (MainPresenter)presenter;
	}
	
	@Override
	/**
	 * Instantiate the main panel, the map, put everything together and return a new viewport with it.
	 */
	public Viewport getViewPort() {
		BorderLayoutData center = new BorderLayoutData(LayoutRegion.CENTER);  
		center.setMargins(new Margins(5, 5, 0, 5));  
		
		BorderLayoutData resultPanel = new BorderLayoutData(LayoutRegion.SOUTH, 250);
		resultPanel.setHidden(true);
		resultPanel.setSplit(true);  
		resultPanel.setCollapsible(true);
		
		Viewport vp = new Viewport();
		vp.setLayout(layout);
	    
		//Create and hook map view and presenter
		this.mapView = new MapViewImpl();
		Util.mapView = mapView;
		new MapPresenter(mapView);
		
		//Init the map view
		mapView.setMapCenter(LatLng.newInstance(-25.430563,-49.272995));
		mapView.getMapWidget().setZoomLevel(15);
		
		//Create the main panel
		
		mainPanel.setLayout(new FitLayout());
		mainPanel.add(mapView.getMapWidget());
		this.buildToolbars();
		
		vp.add(mainPanel,center);
		
		return vp;
	}

	private void buildToolbars(){
		ToolBar topTb = new ToolBar();
		
		/** New places search component */

		Button btnDBSearch = new Button("no cadastro");

		Button googleSearchButton = new Button("no google",new SelectionListener<ButtonEvent>() {  
			public void componentSelected(ButtonEvent ce) {
				mapView.clearLocalResults();
				presenter.performLocalSearch(mapView.getMapCenter(), searchCriteria.getValue());
			}
		});

		Button clearButton = new Button("limpar",new SelectionListener<ButtonEvent>() {  
			public void componentSelected(ButtonEvent ce) {
				mapView.clearLocalResults();
			}
		});
		
		topTb.add(new AdapterField(searchCriteria));
		topTb.add(new SeparatorToolItem());
		topTb.add(new AdapterField(googleSearchButton));
		topTb.add(new AdapterField(btnDBSearch));
		topTb.add(new SeparatorToolItem());
		topTb.add(new AdapterField(clearButton));
		
		//Center on...
		final TextField<String> txtField = new TextField<String>();
		txtField.setWidth(150);
		txtField.setEmptyText("Centralizar mapa em...");

		Button centerButton = new Button("centralizar",new SelectionListener<ButtonEvent>() {  
			public void componentSelected(ButtonEvent ce) {
				//findAddress(txtField.getValue());
			}
		});

		topTb.add(new SeparatorToolItem());
		topTb.add(new AdapterField(txtField));
		topTb.add(new AdapterField(centerButton));
		
		this.mainPanel.setTopComponent(topTb);
	}
}
